package edu.npu.Test;

import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Scanner;

import edu.npu.GraphIndex.BasicGraphBFS;
import edu.npu.GraphIndex.CntVid;
import edu.npu.GraphIndex.HistogramInit;
import edu.npu.GraphQuery.SPGraphQuery;

//用来测试单机graphQuery的类
public class GraphQueryTest {
	public static void main(String[] args) throws FileNotFoundException {
		String qGPath = "/home/locker/graph/query";
		String gGPath = "/home/locker/graph/graph";
		String qLPath = "/home/locker/graph/qLabel";
		String gLPath = "/home/locker/graph/gLabel";
		Map<Long, List<Long>> graph = new HashMap<Long, List<Long>>();
		Map<Long, List<Long>> qGraph = new HashMap<Long, List<Long>>();
		Map<String, List<Long>> lookupTable = new HashMap<String, List<Long>>();
		Map<Long, String> qLabel = new HashMap<Long, String>();
		Map<Long, String> gLabel = new HashMap<Long, String>();
		Map<Long, Map<Integer, Map<String, CntVid>>> histogramIndex = 
			new HashMap<Long, Map<Integer,Map<String,CntVid>>>(); 
		Map<Long, Map<Integer, Map<String, CntVid>>> qHistogramIndex = 
			new HashMap<Long, Map<Integer,Map<String,CntVid>>>();
		Scanner scanner = new Scanner(new File(gGPath));
		long node = -1; String l = null;
		while(scanner.hasNext()) {
			node = scanner.nextLong();
			if(!graph.containsKey(node))
				graph.put(node, new ArrayList<Long>());
			graph.get(node).add(scanner.nextLong());
		}
		scanner = new Scanner(new File(qGPath));
		while(scanner.hasNext()) {
			node = scanner.nextLong();
			if(!qGraph.containsKey(node))
				qGraph.put(node, new ArrayList<Long>());
			qGraph.get(node).add(scanner.nextLong());
		}
		scanner = new Scanner(new File(qLPath));
		while(scanner.hasNext()) {
			node = scanner.nextLong();
			qLabel.put(node, scanner.next());
		}
		scanner = new Scanner(new File(gLPath));
		while(scanner.hasNext()) {
			node = scanner.nextLong();
			l = scanner.next();
			if(!lookupTable.containsKey(l))
				lookupTable.put(l, new ArrayList<Long>());
			lookupTable.get(l).add(node);
			gLabel.put(node, l);
		}
		BasicGraphBFS histBgb = new BasicGraphBFS();
		HistogramInit histInit = new HistogramInit();
		for(Entry<Long, String> entry : gLabel.entrySet()) {
			if(!histogramIndex.containsKey(entry.getKey())) {
				histogramIndex.put(entry.getKey(), new HashMap<Integer, Map<String, CntVid>>());
			}
			histInit.histogram = histogramIndex.get(entry.getKey());
			//建立遍历队列
			for(long n : graph.get(entry.getKey())) {
				histInit.queue.add(n);
			}
			histBgb.queue = histInit.queue;
			histInit.label = gLabel;
			histInit.traversed.add(entry.getKey());
			histBgb.traverse(graph, histInit);
			histInit.clear();
		}
		BasicGraphBFS qHistBgb = new BasicGraphBFS();
		HistogramInit qHistInit = new HistogramInit();
		for(Entry<Long, String> entry : qLabel.entrySet()) {
			if(!qHistogramIndex.containsKey(entry.getKey())) {
				qHistogramIndex.put(entry.getKey(), new HashMap<Integer, Map<String, CntVid>>());
			}
			qHistInit.histogram = qHistogramIndex.get(entry.getKey());
			//建立遍历队列
			for(long n : qGraph.get(entry.getKey())) {
				qHistInit.queue.add(n);
			}
			qHistBgb.queue = qHistInit.queue;
			qHistInit.label = qLabel;
			qHistInit.traversed.add(entry.getKey());
			qHistBgb.traverse(qGraph, qHistInit);
			qHistInit.clear();
		}
		SPGraphQuery spGQuery = new SPGraphQuery(qGraph, 
				lookupTable, qLabel, histogramIndex, qHistogramIndex);
		spGQuery.queryProcessing();
		System.exit(0);
	}
}
